package 非线性结构;

import 非线性结构.树.BinarySearchTree;
import 非线性结构.树.printer.BinaryTrees;

public class Main {
    public static void main(String[] args) {
//        Integer data[] = new Integer[]{
//                7, 4, 9, 2, 5, 8, 11, 3, 12, 1
//        };
//
//        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
//        for (int i = 0; i < data.length; i++) {
//            bst.add(data[i]);
//        }
//
//        BinaryTrees.println(bst);
//        System.out.print("前序遍历");
//        bst.preOrderTraversal(new BinarySearchTree.Visitor<Integer>() {
//            @Override
//            public boolean visit(Integer element) {
//                System.out.print(element+"_");
//                if (element == 4) return true;
//                return false;
//            }
//        });
//        System.out.print("\n中序遍历");
//        bst.inOrderTraversal(new BinarySearchTree.Visitor<Integer>() {
//            @Override
//            public boolean visit(Integer element) {
//                System.out.print(element+"_");
//                if (element == 3) return true;
//                return false;
//            }
//        });
//        System.out.print("\n后序遍历");
//        bst.postorderTraversal(new BinarySearchTree.Visitor<Integer>() {
//            @Override
//            public boolean visit(Integer element) {
//                System.out.print(element+"_");
//                if (element == 5) return true;
//                return false;
//            }
//        });
//        System.out.print("\n层序遍历");
//        bst.levelOrderTraversal(new BinarySearchTree.Visitor<Integer>() {
//            @Override
//            public boolean visit(Integer element) {
//                System.out.print(element+"_");
//                if (element == 5) return true;
//                return false;
//            }
//        });
        test();
    }

    static void test() {
        Integer data[] = new Integer[]{
                7, 4, 9, 2, 5, 8, 11, 3, 1
        };

        BinarySearchTree<Integer> bst2 = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst2.add(data[i]);

        }

        BinarySearchTree<Integer> bst1 = new BinarySearchTree<>();
        for (int i = 0; i < 10; i++) {
            bst1.add((int)(Math.random() * 100));
        }


        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < 10; i++) {
            bst.add((int)(Math.random() * 100));
        }

        BinaryTrees.println(bst);
        System.out.println("bst height"+bst.height());
        BinaryTrees.println(bst1);
        System.out.println("bst1 height"+bst1.height());
        BinaryTrees.println(bst2);
        System.out.println("bst2 height" + bst2.height());
        System.out.println("bst isComplete" + bst.isComplete());
        System.out.println("bst1 isComplete" + bst1.isComplete());
        System.out.println("bst2 isComplete" + bst2.isComplete());

    }

    private static void testPreAndSuc() {
        /**
         * ┌──7──┐
         * │     │
         * ┌─4─┐ ┌─9─┐
         * │   │ │   │
         * ┌─2─┐ 5 8   11─┐
         * │   │          │
         * 1   3          12
         */
        Integer data[] = new Integer[]{
                7, 4, 9, 2, 5, 8, 11, 3, 12, 1
        };

        BinarySearchTree<Integer> bst2 = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst2.add(data[i]);

        }
        BinarySearchTree.Node<Integer> node = (BinarySearchTree.Node<Integer>) bst2.root();
        BinarySearchTree.Node<Integer> node1 = node.right.left;
        System.out.println("node1 element:" + node1.element);
        System.out.println("bst2 predecessor:" + bst2.predecessor(node1).element);
        System.out.println("bst2 successor:" + bst2.successor(node1).element);

    }
}
